<script setup lang="ts">
import {
  delRoleUserAPI,
  getRoleDetailAPI,
  getRoleListAPI,
  getRoleUserAPI,
  getTreeListAPI,
} from "@/apis/system";
import type { Role, RoleData, RoleUser } from "@/types/system";
import { onMounted, ref } from "vue";
import userImage from "@/assets/user.svg";
import userActiveImage from "@/assets/user-active.svg";
import { ElMessage, ElMessageBox, type TreeInstance } from "element-plus";
import { useRouter } from "vue-router";
// RBAC -> Role - Based Access Control 的缩写，意思是基于角色的访问控制。

// 1. 获取角色列表
const roleList = ref<Role[]>([]);
const getRoleList = async () => {
  // 1.1 获取角色列表
  const res = await getRoleListAPI();
  roleList.value = res.data;
  // 1.2 获取第一个角色的权限列表
  const defalutRoleId = roleList.value[0].roleId;
  if (defalutRoleId) {
    getRoleDetail(defalutRoleId);
    getRoleUser(defalutRoleId);
  }
};

// 2. 点击激活高亮交互
const activeIndex = ref(0);
const changeActiveIndex = (index: number) => {
  activeIndex.value = index;
  // 根据索引只获取 roleId
  const roleId = roleList.value[index].roleId;
  // 如果有 roleId 就查询权限信息
  if (roleId) {
    getRoleDetail(roleId);
    getRoleUser(roleId);
  }
};

// 3. 获取权限列表 - 树形结构数据
const treeList = ref<RoleData[]>([]);
const getTreeList = async () => {
  const res = await getTreeListAPI();
  treeList.value = res.data;
  addDisabled(treeList.value);
};

// 4. 添加禁用状态（递归调用-自己调用自己）
const addDisabled = (children: RoleData[]) => {
  children.forEach((item) => {
    // 添加禁用状态
    item.disabled = true;
    // 如果还有子项
    if (item.children) {
      // 递归调用-自己调用自己
      addDisabled(item.children);
    }
  });
};

// 5. 默认勾选权限点
const perms = ref<number[][]>([]);
// 获取 tree 组件实例，用于实现选中，注意这里写到 v-for 中，所以获取到的是数组
const treeRef = ref<TreeInstance[]>();
// 获取权限列表
const getRoleDetail = async (roleId: number) => {
  // 5.1 获取并保存权限列表
  const res = await getRoleDetailAPI(roleId);
  perms.value = res.data.perms;
  // 5.2 遍历所有树形组件实例，高亮 tree 节点
  treeRef.value?.forEach((treeInstance, index) => {
    // 设置树形组件的选中状态
    treeInstance.setCheckedKeys(perms.value[index]);
  });
};

// 6. 切换 tabs
const activeName = ref<"tree" | "member">("tree");

// 7. 获取当前角色下的成员
const roleUserList = ref<RoleUser[]>([]);
const roleUserTotal = ref(0);
const getRoleUser = async (roleId: number) => {
  const res = await getRoleUserAPI(roleId);
  roleUserList.value = res.data.rows;
  roleUserTotal.value = res.data.total;
};

// 8. 编辑角色
const router = useRouter();
const onEditRole = (roleId: number) => {
  // 编辑和添加复用同一个表单页，编辑时把角色 id 传过去
  router.push("/roleAdd?roleId=" + roleId);
};
// 9. 删除角色
const onDeleteRole = async (roleId: number) => {
  try {
    // 二次确认弹窗
    await ElMessageBox.confirm(`确定删除该角色？`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    });
    // 删除角色
    await delRoleUserAPI(roleId);
    // 删除成功，本地删除 或 重新发送请求
    roleList.value = roleList.value.filter((v) => v.roleId !== roleId);
    // 轻提示
    ElMessage.success("删除成功");
  } catch {}
};

onMounted(() => {
  getRoleList();
  getTreeList();
});
</script>

<template>
  <div class="role-container">
    <!-- 左侧角色列表 -->
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: activeIndex === index }"
        @click="changeActiveIndex(index)"
      >
        <div class="role-info">
          <img
            :src="activeIndex === index ? userActiveImage : userImage"
            class="icon"
          />
          <el-tooltip effect="dark" :content="item.remark" placement="top">
            <span class="name">{{ item.roleName }}</span>
          </el-tooltip>
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <img src="@/assets/more.svg" class="icon" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="onEditRole(item.roleId)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item @click="onDeleteRole(item.roleId)">
                  <el-text type="danger">删除</el-text>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="small" @click="$router.push('/roleAdd')">
        添加角色
      </el-button>
    </div>
    <!-- 右侧角色权限 -->
    <div class="right-wrapper">
      <!-- tabs改造结构 -->
      <el-tabs v-model="activeName">
        <el-tab-pane name="tree" label="功能权限">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="treeRef"
                node-key="id"
                :data="item.children"
                :props="{ label: 'title' }"
                show-checkbox
                default-expand-all
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserTotal})`">
          <!-- 成员表格 -->
          <el-table :data="roleUserList">
            <el-table-column label="序号" type="index" width="250" />
            <el-table-column label="员工姓名" prop="name" />
            <el-table-column label="登录帐号" prop="userName" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      // & 引用选择器
      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      .icon {
        width: 15px;
      }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
